(function() {
	var indexItem = 0;
	$.MsgBox = {
		Alert: function(title, msg) {
			GenerateHtml("alert", title, msg);
			btnOk(); //alert只是弹出消息，因此没必要用到回调函数callback
			btnNo();
		},
		Confirm: function(title, msg, callback) {
			GenerateHtml("confirm", title, msg);
			btnOk(callback);
			btnNo();
		},
		push: function(config, callback) {
			getPostHtml(config);
			pushBtnOk(callback);
			btnNo();

		},
		newMd: function(callback) {
			getNewPostHtml();
			newBtnOk(callback);
			btnNo();

		},

		menu: function(array, x, y) {
			getMenuHtml();
			var itemString = '';
			array.forEach((item, index, array) => {
				itemString += '<div class="item"> <span class="aClass">' + item.name + '</span> ';
				itemString += '</div>';

			})
			$("#items").append(itemString);

			$("#mb_con").css({
				top: (y + 39) + "px",
				left: x + "px"
			})
			MenuCssItem();
			btnNo();
			MenuClick(function(index) {
				array[index].click();
			})
		},
		wellcome: function(TopMenuClick) {
			getWellcomeHtml('欢迎使用会飞的猪在线MD');
			let array = TopMenuClick.wellcomeData
			var itemString = '';
			array.forEach((item, index, array) => {
				itemString += '<div class="item"> <span class="aClass">' + item.name + '</span> ';
				itemString += '</div>';

			})
			$("#items").append(itemString);
			ListItemCss();
		
			itemClick(function(index) {
				array[index].click();
			}, "items")
			btnNo();
		},
		// loadCloudList: function(y,itemCallback, queryFC) {
		// 	getUserHtml();
		// 	$("#mb_box").click(function() {
		// 		$("#mb_box,#mb_con").remove();
		// 	})
		// 	loadCloudList(itemCallback, queryFC)
		// },
		
	}
	var loadCloudList= function(itemCallback, queryFC){
		showLoading()
		queryFC.queryCallBack(function(array) {
			hideLoading();
			var itemString = '';
			if (array == null) {
				itemString += '<span style="color: red">数据不存在！</span>';
		
			} else {
				array.forEach((item, index, array) => {
					itemString += '<div class="item"> <span class="aClass">' + item.name + '.md</span> ';
					itemString += '<span>作者: ' + item.UserId + '</span>';
					itemString += '<span>' + getTime(item.createdAt) + '</span>';
					itemString += '</div>';
		
				})
				itemString += '<div id="btnGroup">';
				if (array.length >= queryFC.pageBean.page) {
					itemString += '<span id="next" class="bottombtn">下一页</span>';
		
				}
				if (queryFC.pageBean.current > 1) {
					itemString += '<span id="up" class="bottombtn">上一页</span>';
				}
				itemString += '</div>';
			}
		
			$("#items").append(itemString);
			ListItemCss();
			itemClick(function(index){
				itemCallback(array[index])
			}, "items")
			$("#up").click(function() {
				$("#items").empty();
				
				queryFC.pageBean.current = queryFC.pageBean.current - 1
				loadCloudList(itemCallback, queryFC)
			})
			$("#next").click(function() {
				$("#items").empty();
				queryFC.pageBean.current = queryFC.pageBean.current + 1
				loadCloudList(itemCallback, queryFC)
			})
		})
	}
	var MenuCssItem = function() {

		$(".item").css({
			padding: '10px',
			borderRadius: '5px',
			backgroundColor: '#fff',
			width: '100%',
			cursor: 'pointer'
		})
		//右上角关闭按钮hover样式
		$(".item").hover(function() {
			$(this).css({
				color: '#fff',
				backgroundColor: '#1E90FF'
			});
		}, function() {
			$(this).css({
				backgroundColor: '#fff',
				color: 'black'
			});
		});
	}
	var MenuClick = function(callback) {
		$("#mb_box").click(function() {
			$("#mb_box,#mb_con").remove();
		})
		itemClick(callback, "items");
	}
	var getTime = function(time) {
		return new Date(+new Date(time) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
	}
	
	//生成Html
	var GenerateHtml = function(type, title, msg) {
		var _html = "";
		_html += '<div id="mb_box"></div><div id="mb_con"><span id="mb_tit">' + title + '</span>';
		_html += '<a id="mb_ico">x</a><div id="mb_msg">' + msg + '</div><div id="mb_btnbox">';
		if (type == "alert") {
			_html += '<input id="mb_btn_ok" type="button" value="确定" />';
		}
		if (type == "confirm") {
			_html += '<input id="mb_btn_ok" type="button" value="确定" />';
			_html += '<input id="mb_btn_no" type="button" value="取消" />';
		}
		_html += '</div></div>';
		//必须先将_html添加到body，再设置Css样式
		$("body").append(_html);
		//生成Css
		GenerateCss();
	}

	//生成Html
	var getWellcomeHtml = function(title) {
		var _html = "";
		_html += '<div id="mb_box"></div><div id="mb_con"><span id="mb_tit">' + title + '</span>';
		_html +=
			'<a id="mb_ico">x</a><div id="mb_msg">服务器空间有限，仅限白名单识别码使用图床,工具内置了第三方图床,点击工具栏头像,或者小飞机了解</div><div id="mb_btnbox">';
		_html += '<div id="items">'
		_html += ' </div>';
		_html += '</div></div>';
		//必须先将_html添加到body，再设置Css样式
		$("body").append(_html);
		//生成Css
		GenerateCss();

	}
	
	//生成登录Html
	var getMenuHtml = function() {

		var _html = "";
		_html += '<div id="mb_box"></div><div id="mb_con">';
		_html += '<div id="mb_btnbox">';
		_html += '<div id="items">'
		_html += ' </div>';
		_html += '</div></div>';
		//必须先将_html添加到body，再设置Css样式
		$("body").append(_html);
		//生成Css
		MenuCss();

	}
	//生成登录Html
	var getUserHtml = function() {

		var _html = "";
		_html += '<div id="mb_box"></div><div id="mb_con"><span id="mb_tit">我的在线空间</span>';
		_html += '<div id="mb_btnbox">';
		_html += '<div id="items">'
		_html += ' </div>';
		_html += '</div></div>';
		//必须先将_html添加到body，再设置Css样式
		$("body").append(_html);
		//生成Css
		GenerateCss();

	}
	//生成Html
	var getNewPostHtml = function() {

		var _html = "";
		_html += '<div id="mb_box"></div><div id="mb_con"><span id="mb_tit">新建Md文件</span>';
		_html += '<a id="mb_ico">x</a><div id="mb_btnbox">';

		_html += '<div id="items">'
		_html +=
			'<span class="lab">名称(必填)</span><input id="search-input1" class="inputClass" placeholder="输入文件名称" value=""></input>';
		_html +=
			'<span class="lab">识别码(非必填)</span><input id="search-input2" class="inputClass" placeholder="输入识别码" value=""></input>';
		_html += ' </div>';
		_html += '<input id="mb_btn_ok" type="button" value="确定" />';
		_html += '<input id="mb_btn_no" type="button" value="取消" />';
		_html += '</div></div>';
		//必须先将_html添加到body，再设置Css样式
		$("body").append(_html);
		//生成Css
		GenerateCss();

	}
	//生成Html
	var getPostHtml = function(config) {

		var _html = "";
		_html += '<div id="mb_box"></div><div id="mb_con"><span id="mb_tit">' + config.title + '</span>';
		_html += '<a id="mb_ico">x</a><div id="mb_btnbox">';

		_html += '<div id="items">'
		_html += '<span class="lab">名称</span><input id="search-input1" class="inputClass" placeholder="输入文件名称" value="' +
			config.name + '"></input>';
		_html += '<span class="lab">识别码</span><input id="search-input2" class="inputClass" placeholder="输入识别码" value="' +
			config.code + '"></input>';
		//	_html += '<div><input  type="checkbox" id="cb_btn" checked="true" value="true">覆盖服务器中相同文件</input></div>';
		_html += ' </div>';
		_html += '<input id="mb_btn_ok" type="button" value="确定" />';
		_html += '<input id="mb_btn_no" type="button" value="取消" />';
		_html += '</div></div>';
		//必须先将_html添加到body，再设置Css样式
		$("body").append(_html);
		//生成Css
		GenerateCss();

	}
	//生成Css
	var ListItemCss = function() {
		$("#btnGroup").css({
			width: '100%',
			display: 'flex',
			flexDirection: 'row-reverse',
			marginTop: '15px',
			paddingRight: '25px'

		})
		$(".item").css({
			width: '90%',
			marginBottom: '5px',
			display: 'flex',
			flexDirection: 'row',
			justifyContent: 'space-between',
			padding: '15px',
			borderRadius: '5px',
	        border: '1px dashed #1E90FF',
			background: '#fff',
			cursor: 'pointer',
           
		});
		$(".bottombtn").css({
			cursor: 'pointer',
			display: 'flex',
			flexDirection: 'row',
			justifyContent: 'space-between',
			padding: '5px 10px',
			marginRight: '10px',
			borderRadius: '5px',
			border: '1px dashed #1E90FF',
			
		})
		$(".item ,.bottombtn").hover(function() {
			$(this).css({
				border: '1px dashed #fff',
				backgroundColor: '#1E90FF',
				color:'#fff',
				
			});
		}, function() {
			$(this).css({
				border: '1px dashed #1E90FF',
				backgroundColor: '#fff',
				color:'black'
			});
		});
	}
	//生成Css
	var ListItem3DCss = function() {
		$("#btnGroup").css({
			width: '100%',
			display: 'flex',
			flexDirection: 'row-reverse',
			marginTop: '15px',
			paddingRight: '25px'
	
		})
		$(".item").css({
			width: '90%',
			marginBottom: '5px',
			display: 'flex',
			flexDirection: 'row',
			justifyContent: 'space-between',
			padding: '15px',
			borderRadius: '5px',
	        boxShadow: '0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24)',
			background: '#fff',
	       
		});
		$(".bottombtn").css({
			display: 'flex',
			flexDirection: 'row',
			justifyContent: 'space-between',
			padding: '5px 10px',
			marginRight: '10px',
			borderRadius: '5px',
			boxShadow: '0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24)',
		})
		$(".item ,.bottombtn").hover(function() {
			$(this).css({
				boxShadow: '0 14px 28px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22)',
				transition: 'all 0.3s cubic-bezier(.25,.8,.25,1)',
				cursor: 'pointer',
				backgroundColor: '#1E90FF',
				color:'#fff'
			});
		}, function() {
			$(this).css({
				boxShadow: '0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24)',
				backgroundColor: '#fff',
				color:'black'
			});
		});
	}
	var MenuCss = function() {
		DialogthemeCss1();

		$("#items").css({
			display: 'flex',
			flexDirection: 'column',
			fontSize: '12px',
			alignItems: 'center',
			marginLeft: '10px',
			marginRight: '10px'
		});




	}
	var DialogthemeCss1 = function() {
		$("#mb_box").css({
			width: '100%',
			height: '100%',
			zIndex: '99999',
			position: 'fixed',
			filter: 'Alpha(opacity=60)',
			top: '0',
			left: '0',
			color: '#666'

		});
		$("#mb_con").css({
			zIndex: '999999',
			position: 'fixed',
			backgroundColor: 'White',
			borderRadius: '5px',
			boxShadow: '0 0 10px rgba(0, 0, 0, 0.3)',
			padding: '5px'
		});

		$("#mb_tit").css({
			display: 'block',
			fontSize: '14px',
			color: '#666',
			fontWeight: 'bold',
			textAlign: 'center',
			padding: '6px'
		});
		$("#mb_ico").css({
			display: 'block',
			position: 'absolute',
			right: '10px',
			top: '9px',
			width: '18px',
			height: '18px',
			textAlign: 'center',
			lineHeight: '16px',
			cursor: 'pointer',
			fontFamily: '微软雅黑',
			padding: '6px'
		});
	}
	var DialogthemeCss = function() {
		$("#mb_box").css({
			width: '100%',
			height: '100%',
			zIndex: '99999',
			position: 'fixed',
			filter: 'Alpha(opacity=60)',
			backgroundColor: 'black',
			top: '0',
			left: '0',
			opacity: '0.6'
		});
		$("#mb_con").css({
			zIndex: '999999',
			width: '400px',
			position: 'fixed',
			backgroundColor: 'White',
			borderRadius: '15px'
		});

		$("#mb_tit").css({
			display: 'block',
			fontSize: '14px',
			color: '#444',
			padding: '10px 15px',
			backgroundColor: '#DDD',
			borderRadius: '15px 15px 0 0',
			borderBottom: '3px solid #009BFE',
			fontWeight: 'bold'
		});
		$("#mb_ico").css({
			display: 'block',
			position: 'absolute',
			right: '10px',
			top: '9px',
			border: '1px solid Gray',
			width: '18px',
			height: '18px',
			textAlign: 'center',
			lineHeight: '16px',
			cursor: 'pointer',
			borderRadius: '12px',
			fontFamily: '微软雅黑'
		});
	}
	//生成Css
	var GenerateCss = function() {
		DialogthemeCss();
		$("#mb_msg").css({
			padding: '20px',
			lineHeight: '20px',
			borderBottom: '1px dashed #DDD',
			fontSize: '13px'
		});

		$("#mb_btnbox").css({
			margin: '15px 0 10px 0',
			textAlign: 'center'
		});
		$("#mb_btn_ok,#mb_btn_no").css({
			width: '85px',
			height: '30px',
			color: 'white',
			border: 'none'
		});
		$("#mb_btn_ok").css({
			backgroundColor: '#168bbb'
		});
		$("#mb_btn_no").css({
			backgroundColor: 'gray',
			marginLeft: '20px'
		});

		$("#items").css({
			display: 'flex',
			flexDirection: 'column',
			padding: '15px',
			fontSize: '12px',
			alignItems: 'center',
			borderBottom: '1px dashed #DDD',
			marginBottom: '15px'
		});
		$(".lab").css({
			width: '100%',
			textAlign: 'left',
			marginLeft: '50px',
			padding: '5px'
		})
		//右上角关闭按钮hover样式
		$("#mb_ico").hover(function() {
			$(this).css({
				backgroundColor: 'Red',
				color: 'White'
			});
		}, function() {
			$(this).css({
				backgroundColor: '#DDD',
				color: 'black'
			});
		});
		$(".inputClass").css({
			outlineStyle: 'none',
			border: '1px solid #ddd',
			borderRadius: '3px',
			marginBottom: '10px',
			height: '30px',
			width: '88%',
			padding:'7px',
			border: '1px dashed #ddd'
		})
		
			$(".inputClass").focus(function() {
			$(this).css({
			  border: '1px dashed #ddd'
			});
		}, function() {
			$(this).css({
				border: '1px dashed rgb(30, 144, 255)'
			});
		});
		
	
		centerDialog()
	}
	var centerDialog = function() {
		var _widht = document.documentElement.clientWidth; //屏幕宽
		var _height = document.documentElement.clientHeight; //屏幕高
		var boxWidth = $("#mb_con").width();
		var boxHeight = $("#mb_con").height();
		//让提示框居中
		$("#mb_con").css({
			top: (_height - boxHeight) / 2 + "px",
			left: (_widht - boxWidth) / 2 + "px"
		});
	};
	//确定按钮事件
	var btnOk = function(callback) {
		$("#mb_btn_ok").click(function() {
			$("#mb_box,#mb_con").remove();
			if (typeof(callback) == 'function') {
				callback();
			}
		});
	}

	var newBtnOk = function(callback) {
		$("#mb_btn_ok").click(function() {

			let name = $("#search-input1").val().trim();
			let code = $("#search-input2").val().trim();
			if (name == null || name == '') {
				alert('文件名称不能为空')
				return
			}
			$("#mb_box,#mb_con").remove();
			if (typeof(callback) == 'function') {
				callback(name, code);
			}
		});
	}
	var pushBtnOk = function(callback) {
		$("#mb_btn_ok").click(function() {

			let name = $("#search-input1").val().trim();
			let code = $("#search-input2").val().trim();
			if (name == null || name == '') {
				alert('文件名称不能为空')
				return
			}
			if (code == null || code == '') {
				alert('识别码不能为空')
				return
			}
			$("#mb_box,#mb_con").remove();
			if (typeof(callback) == 'function') {
				callback(name, code, true);
			}
		});
	}
	var itemClick = function(itemCallback, id) {
		$("#" + id).children(".item").click(function() {
			$("#mb_box,#mb_con").remove();
			if (typeof(itemCallback) == 'function') {
				itemCallback($(this).index());
			}
		})
	}
	
	var showLoading = function() {
		$("#mb_btnbox").append(
			'<img id="loading_img" src="./images/loading.gif" style="position: absolute; top: 50%;left:50%"; right: 50%></img>');
	}
	var hideLoading = function() {
		$("#loading_img").remove();
	}

	//取消按钮事件
	var btnNo = function() {
		$("#mb_btn_no,#mb_ico").click(function() {
			$("#mb_box,#mb_con").remove();
		});
	}
})();
